package org.dreamwork.jasmine2.web.controls;

import org.dreamwork.jasmine2.configure.JasmineConfig;
import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.i18n.I18nUtil;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.peony.DataBinder;
import org.dreamwork.util.StringUtil;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-22
 * Time: 23:24:48
 */
public class Label extends WebControl {
    protected String text;
    private String format;

    public void init () {
        htmlTagName = "label";
        super.init ();
    }

    public String getText () {
        return text;
    }

    public void setText (String text) {
        this.text = text;
        if (viewStateEnabled)
            stateManager.put ("text", text);
    }

    public String getFormat () {
        return format;
    }

    public void setFormat (String format) {
        this.format = format;
    }

    public void onDataBind (Object sender) throws EventException {
        super.onDataBind (sender);
        if (dataSource != null || valueField != null) {
            Object value = DataBinder.eval (dataSource, valueField);
            String text;
            if (!StringUtil.isEmpty (format)) {
                text = StringConverter.format (value, format);
            } else
                text = value == null ? "" : value.toString ();
            setText (text);
        }
    }

    protected void loadViewState (Object savedState) throws IOException {
        stateManager.loadViewState (savedState);
        String text = (String) stateManager.get ("text");
        if (text != null)
            setText (text);
    }

    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        super.onPrerender (sender, context);
        attrs.remove ("value");
        attrs.remove ("id");

        if (!StringUtil.isEmpty (text)) {
            if (context.getApplication ().getAttribute (JasmineConfig.JASMINE_I18N_HOLDER) != null)
                text = I18nUtil.eval (text, this);
            text = I18nUtil.escapeI18n (text);
        }
    }

    public void render (PrintWriter writer) throws IOException, EventException {
        if (!visible) return;
        writer.write ("<label");
        String clientId = getClientId ();
        if (clientId != null)
            writer.write (" id=\"" + clientId + "\"");

        super.renderAttributes (writer);
        writer.write (">");
        writer.print (text == null ? "" : text);
        writer.println ("</" + htmlTagName + ">");
    }

    @Override
    public IWebControl copy () {
        try {
            Label label = (Label) super.baseCopy ();
            if (label != this) {
                label.format = format;
            }
            return label;
        } catch (Exception ex) {
            return super.copy ();
        }
    }
}